#coding=utf-8
'''
Created on Nov 26, 2010
@author: liyinhui
@moduleName: pub.forms
@description: 
'''
from django import forms
from django.forms import PasswordInput
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm

#class PubBaseForm(ModelForm):
#    NEXT = forms.CharField(widget=forms.HiddenInput(attrs={'id':'NEXT'}))

class RegForm(UserCreationForm):
    username = forms.CharField(label="邮箱", max_length=30,
        widget=forms.TextInput(attrs={'onfocus': 'Mail.focus(this)',
                                      'onblur': 'Mail.check(this)',
                                      'class': 'txt3', 'value': 'lyh1@lyh1.com'}), )
    password1 = forms.CharField(label="密码", max_length=15,
        widget=PasswordInput(attrs={'class': 'txt3',
                                    'onblur': 'Pwd.check(this,1)',
                                    'onfocus': 'Pwd.focus(this,1)', 'value': '123456'}), )
    password2 = forms.CharField(label="确认密码", max_length=15,
        widget=PasswordInput(attrs={'class': 'txt3',
                                    'onkeyup': 'Pwd.keyup(this)', 'onblur': 'Pwd.check(this,2)',
                                    'onfocus': 'Pwd.focus(this,2)', 'value': '123456'}), )
    first_name = forms.CharField(label='昵称', max_length=20,
        widget=forms.TextInput(attrs={'onkeyup': 'Uname.keyup(this)',
                                      'onblur': 'Uname.check(this)',
                                      'onfocus': 'Uname.focus(this)',
                                      'class': 'txt3', 'value': 'lyh1'}), )

    email = forms.EmailField(max_length=70, required=False)

    def clean_first_name(self):
        first_name = self.cleaned_data['first_name']
        if first_name is None:
            raise forms.ValidationError('昵称不能为空！')
        return first_name

    def clean_email(self):
        return self.cleaned_data['username']

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        user.first_name = self.clean_first_name()
        user.email = self.clean_email()
        if commit:
            user.save()
        return user

    class Meta:
        model = User
        fields = ['username', 'password1', 'password2', 'first_name']


class LoginForm(AuthenticationForm):
    username = forms.CharField(label="邮箱", max_length=30,
            widget=forms.TextInput(attrs={'class': 'txt3', 'onblur':'checkUsername(this)'}), )
                                          #,'onfocus': 'Mail.focus(this)',
                                          #'onblur': 'Mail.checkForLogin(this)',
    password = forms.CharField(label="密码", max_length=15,
        widget=PasswordInput(attrs={'class': 'txt3',
                                    'onblur': 'checkPassword(this)'}), )
                                    #'onblur': 'Pwd.check(this,1)',
                                    #'onfocus': 'Pwd.focus(this,1)',
    class Meta:
        model = User
        fields = ['username', 'password']
    